using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Protocol;
using System;
using System.Collections.Generic;
namespace MicroMsg.Network
{
	public class Receiver
	{
		public static Receiver gReceiver;
		public static Receiver getInstance()
		{
			if (Receiver.gReceiver == null)
			{
				Receiver.gReceiver = new Receiver();
			}
			return Receiver.gReceiver;
		}
		public bool handler()
		{
			if (!NetworkDeviceWatcher.isNetworkAvailable())
			{
				return false;
			}
			List<Connector.RawPackBody> list = Connector.receiveBodyList();
			if (list != null && list.get_Count() > 0)
			{
				using (List<Connector.RawPackBody>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Connector.RawPackBody current = enumerator.get_Current();
						this.onReceivedResponsePack(current.body, current.int_1, current.int_0, current.cmdUri);
					}
				}
				return true;
			}
			return false;
		}
		private void onReceivedResponsePack(byte[] body, int int_0, int int_1, string cmdUri)
		{
			Log.smethod_4("Network", string.Concat(new object[]
			{
				"receive a packet , cmd = ",
				(CmdConst)int_1,
				",seq = ",
				int_0,
				" , len = ",
				body.Length
			}));
			SessionPack sessionPackBySeq = SessionPackMgr.getSessionPackBySeq(int_0);
			if (sessionPackBySeq == null)
			{
				FlowControl.onReceive(int_1, "", (uint)body.Length);
				this.onReceivedNotify(int_0, int_1, body);
				return;
			}
			FlowControl.onReceive(sessionPackBySeq.mCmdID, sessionPackBySeq.mCmdUri, (uint)body.Length);
			sessionPackBySeq.cancelTimeoutPoint();
			sessionPackBySeq.mSendStatus = 4;
			if (sessionPackBySeq.mCmdID == 6)
			{
				sessionPackBySeq.mSendStatus = 5;
				return;
			}
			if (sessionPackBySeq.mCmdID == 25)
			{
				sessionPackBySeq.mSendStatus = 5;
				sessionPackBySeq.mResponseBuffer = body;
				OnCallback.onSuccess(sessionPackBySeq);
				return;
			}
			if (sessionPackBySeq.mCmdID == 1)
			{
				SessionPackMgr.setAuthStatus(0);
			}
			if (sessionPackBySeq.mCanceled)
			{
				Log.smethod_4("Network", "receive a packet been cancelled.)");
				return;
			}
			byte[] sessionKey = sessionPackBySeq.getSessionKey(false);
			if (sessionKey == null)
			{
				Log.smethod_1("Network", "ignore a packet without valid key.)");
				return;
			}
			byte[] cookie = null;
			byte[] mResponseBuffer = null;
			int num = 0;
			if (!MMPack.DecodePack(ref mResponseBuffer, body, sessionKey, ref cookie, ref num))
			{
				Log.smethod_1("Network", string.Concat(new object[]
				{
					"unpack error! cmd=",
					(CmdConst)int_1,
					" seq=",
					int_0
				}));
				Connector.close();
				if (num == -13)
				{
					Log.smethod_1("Network", "session timeout");
					SessionPackMgr.setSessionKey(null);
				}
				if (sessionPackBySeq.mRetryLeft > 0)
				{
					sessionPackBySeq.mCacheBodyBuffer = null;
					sessionPackBySeq.cancelTimeoutPoint();
					sessionPackBySeq.mSendStatus = 0;
					return;
				}
				sessionPackBySeq.mSendStatus = 6;
				OnCallback.onError(sessionPackBySeq, PackResult.UNPACK_ERROR);
				return;
			}
			else
			{
				sessionPackBySeq.mSendStatus = 5;
				sessionPackBySeq.mResponseBuffer = mResponseBuffer;
				object obj = null;
				if (!AuthPack.preProcessAuthPack(sessionPackBySeq, ref obj))
				{
					sessionPackBySeq.mSendStatus = 6;
					OnCallback.onAllError(PackResult.AUTH_ERROR);
					Connector.onSendFailed(false, 3);
					return;
				}
				this.updateCookie(sessionPackBySeq.mCmdID, num, cookie);
				if (obj == null)
				{
					try
					{
						sessionPackBySeq.mResponseObject = sessionPackBySeq.onResponseParser();
					}
					catch (Exception ex)
					{
						Log.smethod_1("Network", "onResponseParser failed," + ex.ToString());
						OnCallback.onError(sessionPackBySeq, PackResult.PARSER_ERROR);
						return;
					}
					if (sessionPackBySeq.mResponseObject != null)
					{
						goto IL_249;
					}
					Log.smethod_1("Network", "onResponseParser failed");
					OnCallback.onError(sessionPackBySeq, PackResult.PARSER_ERROR);
					return;
				}
				sessionPackBySeq.mResponseObject = obj;
				IL_249:
				OnCallback.onSuccess(sessionPackBySeq);
				return;
			}
		}
		private void updateCookie(int cmdID, int int_0, byte[] cookie)
		{
			if (cmdID == 8)
			{
				return;
			}
			bool flag = false;
			if (int_0 == 0)
			{
				flag = true;
			}
			if (cmdID == 1 && (int_0 == -16 || int_0 == -17))
			{
				flag = true;
			}
			if (!flag)
			{
				return;
			}
			if (cookie != null)
			{
				SessionPackMgr.setSeverID(cookie);
				return;
			}
			Log.smethod_1("Network", "updateCookie: invalid cookie");
		}
		private void onReceivedNotify(int int_0, int int_1, byte[] data)
		{
			Log.smethod_4("Network", string.Concat(new object[]
			{
				"notify received, cmd=",
				(CmdConst)int_1,
				", datalen=",
				data.Length
			}));
			switch (int_1)
			{
			case 5:
				break;
			case 6:
			case 8:
			case 9:
			case 10:
			case 11:
				return;
			case 7:
				EventCenter.postEvent(EventConst.SYS_MSG_EXIT, null, null);
				return;
			case 12:
				Connector.close();
				return;
			default:
				if (int_1 != 24)
				{
					if (int_1 != 72)
					{
						return;
					}
					ServerBulletin.onRecvBulletin(data);
					return;
				}
				break;
			}
			if (data != null && data.Length >= 4)
			{
				int num = 0;
				int num2 = Util.readInt(data, ref num);
				EventCenter.postEvent(EventConst.ON_NETSCENE_NOTIFY_SYNC_REQ, num2, null);
				return;
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_NOTIFY_SYNC_REQ, null, null);
		}
	}
}
